package com.cloud.service.accesstoken.impl;

import com.cloud.commons.utils.CloudCopyUtils;
import com.cloud.commons.utils.wechat.WeixinUtil;
import com.cloud.dao.accesstoken.AccessTokenDao;
import com.cloud.model.entity.accesstoken.AccessToken;
import com.cloud.service.accesstoken.AccessTokenService;
import com.cloud.commons.constants.ConsumeConstant;
import com.cloud.model.view.accesstoken.AccessTokenView;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Optional;

@Service
public class AccessTokenServiceImpl implements AccessTokenService {
    @Autowired
    private AccessTokenDao accessTokenDao;

    @Autowired
    private AccessTokenService accessTokenService;
    @Override
    public AccessTokenView saveEntity(AccessTokenView entity){
        Long currentTime = System.currentTimeMillis();
        AccessToken accessToken = new AccessToken();
        BeanUtils.copyProperties(entity,accessToken);
        accessToken.setCreateTime(System.currentTimeMillis());
        accessToken.setCreateTime(currentTime);
        accessToken.setUpdateTime(currentTime);
        accessToken.setWeight(currentTime);
        accessToken.setEnabled(ConsumeConstant.ENABLED);

        AccessToken accessTokenReturn = accessTokenDao.save(accessToken);
        BeanUtils.copyProperties(accessTokenReturn,entity);
        return entity;
    }

    @Override
    public void deleteEntity(long id) {

    }

    @Override
    public void deleteEntities(String ids) {

    }

    @Override
    public void updateEntity(AccessTokenView entity){
        Optional<AccessToken> accessTokenOptional = accessTokenDao.findById(entity.getId());
        AccessToken accessToken = accessTokenOptional.orElseThrow(() -> new RuntimeException("获取不到id为：" + entity.getId() + "的数据记录"));
        // 进行copy
        CloudCopyUtils.copyBean(entity,accessToken);
        accessToken.setUpdateTime(System.currentTimeMillis());
        // 进行更新操作
        accessTokenDao.save(accessToken);
    }

    @Override
    public AccessTokenView getEntity(long id){
        return null;
    }

    @Override
    public long getEntitiesCount() {
        return 0;
    }

    @Override
    public List<AccessTokenView> findAll() {
        return null;
    }

    @Override
    public String getAccessToken(){
        List<AccessToken> accessTokenAll = accessTokenDao.findAll();
        // 判断token不存在重新获取token信息
        if (ObjectUtils.isEmpty(accessTokenAll)  && accessTokenAll.size() == 0) {
            AccessToken accessToken = WeixinUtil.getAccessToken();
            AccessTokenView accessTokenView = new AccessTokenView();
            CloudCopyUtils.copyBean(accessToken,accessTokenView);
            accessTokenView.setCreateTime(System.currentTimeMillis());
            accessTokenView.setUpdateTime(System.currentTimeMillis());
            accessTokenView.setWeight(System.currentTimeMillis());
            accessTokenView.setEnabled(ConsumeConstant.ENABLED);
            accessTokenService.saveEntity(accessTokenView);
            return accessToken.getAccessToken();
        }
        // token存在判断是否过期
        AccessToken accessToken = accessTokenAll.get(0);
        Long nowTime = System.currentTimeMillis();
        // 只获取一小时的
        Long expiresTime = accessToken.getUpdateTime() + (accessToken.getExpiresIn() * 1000) / 2;
        // 已过期 重新获取token信息
        if (nowTime > expiresTime) {
            AccessToken accessTokenUpdate = WeixinUtil.getAccessToken();
            AccessTokenView accessTokenView = new AccessTokenView();
            CloudCopyUtils.copyBean(accessToken,accessTokenView);
            accessTokenView.setUpdateTime(System.currentTimeMillis());
            accessTokenView.setAccessToken(accessTokenUpdate.getAccessToken());
            accessTokenView.setExpiresIn(accessTokenUpdate.getExpiresIn());
            accessTokenService.updateEntity(accessTokenView);
            return accessTokenView.getAccessToken();
        }
        return accessToken.getAccessToken();
    }
}
